package com.atguigu.gulimall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * MQ配置
 * 容器中的Binding Queue Exchange都会自动创建
 *
 * @author LaiSanShan
 * @date 2021-09-07
 */
@Configuration
public class MyMQConfig {

    /**
     * 50min后消息会发到stock-event-exchange路由，带键stock.release.order
     *
     * @return 延迟队列
     */
    @Bean
    public Queue stockDelayQueue() {
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "stock-event-exchange");
        arguments.put("x-dead-letter-routing-key", "stock.release.stock");
        arguments.put("x-message-ttl", 120000);
        return new Queue("stock.delay.queue",
                true,
                false,
                false,
                arguments);
    }

    /**
     * 接收50min后的解锁库存消息
     *
     * @return 队列
     */
    @Bean
    public Queue stockReleaseOrderQueue() {
        return new Queue("stock.release.stock.queue",
                true,
                false,
                false);
    }

    /**
     * ware服务事件的交换机
     *
     * @return 交换机
     */
    @Bean
    public Exchange stockEventExchane() {
        return new TopicExchange("stock-event-exchange",
                true,
                false);
    }

    /**
     * stock-event-exchange交换机到stock.delay.queue，根据键stock.create.stock
     *
     * @return 交换机->队列
     */
    @Bean
    public Binding stockCreateStockBinding() {
        return new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",
                null);
    }

    /**
     * stock-event-exchange交换机到stock.release.stock.queue，根据键stock.release.stock
     *
     * @return 交换机->队列
     */
    @Bean
    public Binding stockReleaseStockBinding() {
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.stock",
                null);
    }

}
